package com.lanou.controller;

import com.lanou.entity.VipInfo;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wangxinleibobo
 */
@Controller
@RequestMapping(value = "/login")
public class LoginController {
    /**
     * 接收登录提交的表单
     * @param
     * @param
     * @return
     */
    RestTemplate restTemplate=new RestTemplate();

    @RequestMapping(value = "/loginFrom.do")
    public ModelAndView loginFrom(String vipAccount,String vipPassword) {
        System.out.println("从form表单提交的数据----"+vipAccount+vipPassword);
        ModelAndView modelAndView=new ModelAndView();
        Map<String,Object> map=new HashMap<>();
        map.put("vipAccount",vipAccount);
        map.put("vipPassword",vipPassword);
        //调用登录的接口,获取返回的对象
        MultiValueMap multiValueMap=new LinkedMultiValueMap();
        multiValueMap.setAll(map);
        VipInfo vipInfo = restTemplate.postForObject("http://10.90.87.218:8080/port/viplogin", multiValueMap, VipInfo.class);
        System.out.println("返回的结果----"+vipInfo);
        if(vipInfo!=null){
            modelAndView.setViewName("view/page");
            modelAndView.addObject("vipInfo",vipInfo);
            modelAndView.addObject("result",vipInfo);
        }else {
            modelAndView.setViewName("view/login");
        }
        return modelAndView;
    }

    /**
     * 从主页跳转到登录界面
     * @return
     */
    @RequestMapping(value = "/loginPage.do")
    public String loginFrom() {

        return "view/login";
    }
}
